package com.ncloud.system.service.impl;

import com.ncloud.system.entity.Depart;
import com.ncloud.system.entity.DepartRole;
import com.ncloud.system.entity.Role;
import com.ncloud.system.entity.Sequence;
import com.ncloud.system.mapper.DepartMapper;
import com.ncloud.system.mapper.DepartRoleMapper;
import com.ncloud.system.service.DepartService;
import com.ncloud.system.service.SequenceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Service
public class DepartServiceImpl implements DepartService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DepartMapper departMapper;
    @Autowired
    private DepartRoleMapper departRoleMapper;
    @Autowired
    private SequenceService sequenceService;
    @Override
    public Long insertDepart(Depart depart) {
        depart.setCreateDate(new Date());
        if(depart.getDepartId() == null){
           Long id = sequenceService.getSequence("depart_id_seq");
           depart.setDepartId(id);
        }
        depart.setUseTag("1");
        departMapper.insertDepart(depart);
        return depart.getDepartId();
    }

    @Override
    public Long updateDepart(Depart depart) {
        depart.setUpdateDate(new Date());
        departMapper.updateDepart(depart);
        return depart.getDepartId();
    }

    @Override
    public Depart getOneDepart(Long departId) {
        return departMapper.getOneDepart(departId);
    }

    @Override
    public DepartRole insertDepartRole(DepartRole departRole) {
        departMapper.insertDepartRole(departRole);
        return departRole;
    }

    @Override
    public int deleteDepartRole(Long departId) {
        return departMapper.deleteDepartRole(departId);
    }

    @Override
    public int deleteDepart(Long departId) {
        return departMapper.deleteDepart(departId);
    }

    @Override
    public List<Depart> getDepartsByCompanyId(Long companyId) {
        return departMapper.getDepartsByCompanyId(companyId);
    }

    @Override
    public List<Role> getRolesByDepartId(Long departId) {
        return departMapper.getRolesByDepartId(departId);
    }

    @Override
    public List<Depart> getDepartsAll() {
        return departMapper.getDepartsAll();
    }

    @Override
    public List<DepartRole> getDepartRoleByRole(Long roleId) {
        return departRoleMapper.getDepartRoleByRole(roleId);
    }

    @Override
    public int getDepartAccountNum(Long departId) {
        return departMapper.getDepartAccountNum(departId);
    }

    @Override
    public int deleteDepartRoleState(Long departId) {
        return departMapper.deleteDepartRoletate(departId);
    }

    @Override
    public int deleteDepartState(Long departId) {
        return departMapper.deleteDepartState(departId);
    }
}
